package mvc;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.lang.reflect.Method;

public class WebHandler {
    private Object controllerBeam;

    private Method method;

    private ResultType resultType;

    public WebHandler(Object controllerBeam, Method method) {
        this.controllerBeam = controllerBeam;
        this.method = method;
        this.resultType = resovleResultType(controllerBeam,method);
    }

    private ResultType resovleResultType(Object controllerBeam, Method method) {
        if(method.isAnnotationPresent(ResponseBody.class)){
            return ResultType.JSON;
        }
        if(method.getReturnType() == ModelAndView.class){
            return ResultType.LOCAL;
        }
        return ResultType.HTML;
    }

    public Object getControllerBeam() {
        return controllerBeam;
    }

    public Method getMethod() {
        return method;
    }

    public ResultType getResultType() {
        return resultType;
    }

    public void handle(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        method.invoke(controllerBeam, req, resp);
    }

    enum ResultType{
        JSON,HTML,LOCAL
    }
}
